﻿namespace EasyWeb
{
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using EasyWeb.Data;
    using EasyWeb.Design;
    using EasyWeb.Validation;

    [EWDisplayName(Name = "FieldDefinitionText", ResourceType = typeof(EWFieldDefinitionResources))]
    public class EWFieldDefinitionText : EWFieldDefinition
    {
        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowDefaultValue")]
        public override bool AllowDefaultValue
        {
            get { return true; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowUnique")]
        public override bool AllowUnique
        {
            get { return true; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_DefaultValue")]
        public override object DefaultValue
        {
            get { return this.StateBag.Get<string>("DefaultValue"); }
            set { this.StateBag.Set("DefaultValue", (string)value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionText_MinimumLength")]
        [EWPropertyCompareValidation("MaximumLength", EWCompareMode.LessThanOrEquals)]
        public int? MinimumLength
        {
            get { return this.StateBag.Get<int?>("MinimumLength"); }
            set { this.StateBag.Set("MinimumLength", value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionText_MaximumLength")]
        public int? MaximumLength
        {
            get { return this.StateBag.Get<int?>("MaximumLength"); }
            set { this.StateBag.Set("MaximumLength", value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionText_StoreMode")]
        public EWTextStoreMode StoreMode
        {
            get { return this.StateBag.Get<EWTextStoreMode>("StoreMode", () => EWTextStoreMode.Unicode); }
            set
            {
                EWGuard.IsDefinedEnum(value, "value");
                this.StateBag.Set("StoreMode", value);
            }
        }

        public override EWFieldDefinitionColumnCollection CreateFieldDefinitionColumns()
        {
            var field = new EWFieldDefinitionColumn()
            {
                Name = this.InternalName,
                IsNullable = this.AllowNull,
                IsUnique = this.AllowUnique && this.IsUnique,
                Length = int.MaxValue
            };

            switch (this.StoreMode)
            {
                case EWTextStoreMode.Ansi:
                    field.Type = DbType.AnsiString;
                    break;
                case EWTextStoreMode.Unicode:
                    field.Type = DbType.String;
                    break;
                case EWTextStoreMode.AnsiFixedLength:
                    field.Type = DbType.AnsiStringFixedLength;
                    break;
                case EWTextStoreMode.UnicodeFixedLength:
                    field.Type = DbType.StringFixedLength;
                    break;
            }

            return new EWFieldDefinitionColumnCollection()
            {
                field
            };
        }

        public override void Deserialize(EWSerializationInfo info)
        {
            base.Deserialize(info);

            this.MaximumLength = info.GetNullableInt32("MaximumLength");
            this.MinimumLength = info.GetNullableInt32("MinimumLength");
            this.StoreMode = info.GetEnum<EWTextStoreMode>("StoreMode");
            this.DefaultValue = info.GetString("DefaultValue");
        }

        public override void Serialize(EWSerializationInfo info)
        {
            base.Serialize(info);

            info.AddValue("MaximumLength", this.MaximumLength);
            info.AddValue("MinimumLength", this.MinimumLength);
            info.AddValue("StoreMode", this.StoreMode);
            info.AddValue("DefaultValue", (string)this.DefaultValue);
        }
    }
}
